package com.canbot.u05.view;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaRecorder;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.support.design.button.MaterialButton;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.utils.ToastTools;

import java.io.File;
import java.io.IOException;

/**
 * 迎宾模式文字转语音语音录入对话框
 */

public class UsherModeRecordDialog extends Dialog {

        public final static int USHER_MODE = 1;

        public final static int HOST_MODE = 2;

        public final static int Record = 3;

        public static final String RECORD_FILE_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/record/";

        private Button btnFinish,btnCancle;

        private TextView recordTime;

        private ImageButton loading;

        private AnimationDrawable anim_Load;

        private int recLen = 0;

        private int minute = 0;

        final Handler handler = new Handler(Looper.getMainLooper());

        private MediaRecorder recorder;

        private String fileName;

        private TextView viewById;

        public UsherModeRecordDialog(Context context, int type) {
                super(context, R.style.MyDialogStyle);
                if (type == USHER_MODE) {
                        setCustomDialog();
                }
                else if (type == HOST_MODE) {
                        setHostModeCustomDialog();
                }
                else if (type == Record) {
                        setRecordDialog();
                }

        }

        private void setCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_usher_mode_record_speech_dialog, null);
                recordTime = (TextView) mView.findViewById(R.id.record_time);
                viewById = (TextView) mView.findViewById(R.id.tip);
                btnFinish = (Button) mView.findViewById(R.id.record_dialog_finish);
                btnCancle = (Button) mView.findViewById(R.id.record_dialog_cancle);
                loading = (ImageButton) mView.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.usher_mode_record_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                super.setContentView(mView);
        }

        private void setRecordDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_record_speech_dialog, null);
                recordTime = (TextView) mView.findViewById(R.id.record_time);
                viewById = (TextView) mView.findViewById(R.id.tip);
                btnFinish = (Button) mView.findViewById(R.id.record_dialog_finish);
                btnCancle = (Button) mView.findViewById(R.id.record_dialog_cancle);
                loading = (ImageButton) mView.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.record_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                super.setContentView(mView);
        }

        /**
         * 主持模式录音框
         */
        private void setHostModeCustomDialog() {
                View mView = LayoutInflater.from(getContext()).inflate(R.layout.layout_host_mode_record_speech_dialog, null);
                recordTime = (TextView) mView.findViewById(R.id.record_time);
                viewById = (TextView) mView.findViewById(R.id.tip);
                btnFinish = (Button) mView.findViewById(R.id.record_dialog_finish);
                btnCancle = (Button) mView.findViewById(R.id.record_dialog_cancle);
                loading = (ImageButton) mView.findViewById(R.id.vLoad);
                loading.setBackgroundResource(R.drawable.host_mode_record_load_anim);
                anim_Load = (AnimationDrawable) loading.getBackground();
                anim_Load.start();
                super.setContentView(mView);
        }

        public void setTime(String str) {
                viewById.setText(str);
                btnFinish.setEnabled(false);
                btnCancle.setEnabled(false);
        }

        @Override
        public void show() {
                super.show();
                btnFinish.setEnabled(true);
                btnCancle.setEnabled(true);
                anim_Load.start();
                handler.postDelayed(runnable, 1000);
                startRecorder();
        }

        Runnable runnable = new Runnable() {
                @Override
                public void run() {
                        recLen++;
                        if (recLen > 59) {
                                recLen = 0;
                                minute++;
                                if (recLen < 10) {
                                        recordTime.setText("" + minute + ":0" + recLen);
                                }
                                else {
                                        recordTime.setText("" + minute + ":" + recLen);
                                }
                        }
                        else {
                                if (recLen < 10) {
                                        recordTime.setText("" + minute + ":0" + recLen);
                                }
                                else {
                                        recordTime.setText("" + minute + ":" + recLen);
                                }
                        }
                        handler.postDelayed(this, 1000);
                }
        };

        @Override
        public void setCancelable(boolean flag) {
                super.setCancelable(false);
        }

        @Override
        public void cancel() {
                super.cancel();
                File fileName2 = new File(RECORD_FILE_DIR, fileName);
                if (fileName2.exists()) {
                        fileName2.delete();
                }
        }

        public String ok() {
                stopRecorder();
                minute = 0;
                recLen = 0;
                handler.removeCallbacks(runnable);
                return fileName;
        }

        public void no() {
                cancel();
                stopRecorder();
                minute = 0;
                recLen = 0;
                handler.removeCallbacks(runnable);
                File fileName2 = new File(RECORD_FILE_DIR, fileName);
                if (fileName2.exists()) {
                        fileName2.delete();
                }
        }

        @Override
        public void setContentView(int layoutResID) {
        }

        @Override
        public void setContentView(View view, ViewGroup.LayoutParams params) {
        }

        @Override
        public void setContentView(View view) {
        }


        /**
         * 确定键监听器
         *
         * @param listener
         */
        public void setFinishClickListener(View.OnClickListener listener) {
                btnFinish.setOnClickListener(listener);
        }

        /**
         * 取消键监听器
         *
         * @param listener
         */
        public void setCancleClickListener(View.OnClickListener listener) {
                btnCancle.setOnClickListener(listener);
        }

        /**
         * 录音时长
         */
        public void getRecordTime() {
                recordTime.getText();
        }

        private void startRecorder() {
                recorder = new MediaRecorder();
                recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
                recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
                File fileDIR = new File(RECORD_FILE_DIR);
                if (!fileDIR.exists()) {
                        fileDIR.mkdirs();
                }
                fileName = android.text.format.DateFormat.format("yyyy_MM_dd_HH_ss_mm", System.currentTimeMillis()).toString() + ".mp3";
                recorder.setOutputFile(RECORD_FILE_DIR + fileName);
                try {
                        recorder.prepare();
                }
                catch (IOException e) {
                        e.printStackTrace();
                        ToastTools.newInstance(getContext()).showText( getContext().getString(R.string.recording_abnormal));
                        cancel();
                }
                try {
                        recorder.start();   // Recording is now started
                }
                catch (IllegalStateException e) {
                        e.printStackTrace();
                        ToastTools.newInstance(getContext()).showText(getContext().getString(R.string.recording_failure));
                        cancel();
                }
        }

        private void stopRecorder() {
                if (recorder == null) {
                        return;
                }
                recorder.stop();
                recorder.reset();   // You can reuse the object by going back to setAudioSource() step
                recorder.release(); // Now the object cannot be reused
                recorder = null;
        }
}
